package com.panfeng.xcloud.boss.provider.member.service.impl;

import com.panfeng.xcloud.boss.provider.member.dao.mapper.UserWalletInfoMapperExt;
import com.panfeng.xcloud.boss.provider.member.dto.TronAccountDTO;
import com.panfeng.xcloud.boss.provider.member.service.IUserWalletInfoService;
import com.panfeng.xcloud.common.core.configuration.DataDictionaryConfig;
import com.panfeng.xcloud.common.core.enums.ChainTypeEnum;
import com.panfeng.xcloud.common.core.enums.CoinTypeEnum;
import com.panfeng.xcloud.common.core.enums.DataDictionaryTypeEnum;
import com.panfeng.xcloud.common.core.utils.DES3Util;
import com.panfeng.xcloud.dao.assets.entity.UserWalletInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import trontool.Credential;

import java.math.BigDecimal;

@Service
@Slf4j
public class UserWalletInfoServiceImpl implements IUserWalletInfoService {

    @Autowired
    private UserWalletInfoMapperExt userWalletInfoMapperExt;

    @Autowired
    private DataDictionaryConfig dataDictionaryConfig;

    @Value("${xdcloud.des3.secret-key}")
    private String secrectKey;

    @Override
    public void createUserWalletInfo(String userId) {
        TronAccountDTO result = createTrxAddress();
        String chainName = result.getChainName();
        String coinName = result.getCoinName();
        String privateKey = result.getPrivateKey();
        String publicKey = result.getPublicKey();
        String address = result.getAddress();
        String salt = DES3Util.getRandomIv();

        UserWalletInfo userWalletInfo = new UserWalletInfo();
        userWalletInfo.setUserId(userId);
        userWalletInfo.setChainName(chainName);
        userWalletInfo.setCoinName(coinName);
        userWalletInfo.setAddress(address);
        userWalletInfo.setPrivateKey(DES3Util.doubleEncode(privateKey, secrectKey, salt));
        userWalletInfo.setPublicKey(DES3Util.doubleEncode(publicKey, secrectKey, salt));
        userWalletInfo.setAvaliableBalance(BigDecimal.ZERO);
        userWalletInfo.setFrozenBalance(BigDecimal.ZERO);
        userWalletInfo.setTodayProfit(BigDecimal.ZERO);
        userWalletInfo.setTotalProfit(BigDecimal.ZERO);
        userWalletInfo.setPointBalance(new BigDecimal(dataDictionaryConfig.getCodeToNameMap().get(
                DataDictionaryTypeEnum.new_user_init_point_balance.getCode())));
        userWalletInfo.setMyPointRatio(new BigDecimal(dataDictionaryConfig.getCodeToNameMap().get(DataDictionaryTypeEnum.profit_delete_point_ratio.getCode())));
        userWalletInfo.setSalt(salt);
        String defaultRebateLevel = dataDictionaryConfig.getCodeToNameMap().get(DataDictionaryTypeEnum.default_rebate_level.getCode());
        String rebateLevel = defaultRebateLevel.split(",")[0];
        String[] split = rebateLevel.split("-");
        userWalletInfo.setRebateLevel(split[0]);
        userWalletInfo.setRebateRatio(new BigDecimal(split[1]));
        userWalletInfo.setSubUserTotalProfit(BigDecimal.ZERO);

        userWalletInfoMapperExt.insertSelective(userWalletInfo);
    }

    private TronAccountDTO createTrxAddress() {
        Credential c1 = Credential.create();
        String privateKey = c1.getPrivateKey();
        Credential c2 = Credential.fromPrivateKey(privateKey);
        TronAccountDTO tronAccountDTO = new TronAccountDTO();
        tronAccountDTO.setChainName(ChainTypeEnum.TRON.getCode());
        tronAccountDTO.setCoinName(CoinTypeEnum.USDT.getCode());
        tronAccountDTO.setAddress(c2.getAddress().toString());
        tronAccountDTO.setPublicKey(c2.getPublicKey());
        tronAccountDTO.setPrivateKey(c2.getPrivateKey());
        return tronAccountDTO;
    }

}
